Conditions | 16 |
Total Lines | 80 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like OSPaths.ts ➔ Adapt often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | // # spell-checker:ignore AllUsersProfile HomeDrive HomePath LocalAppData UserProfile WinDir falsey |
||
22 | |||
23 | function Adapt(adapter_: Platform.Adapter): { readonly OSPaths: OSPaths } { |
||
24 | const { env, os, path } = adapter_; |
||
25 | |||
26 | const isWinOS = /^win/i.test(adapter_.process.platform); |
||
27 | |||
28 | function normalizePath(path_: string | undefined): string | undefined { |
||
29 | return path_ ? adapter_.path.normalize(adapter_.path.join(path_, '.')) : void 0; |
||
30 | } |
||
31 | |||
32 | function home() { |
||
33 | const posix = () => |
||
34 | normalizePath((typeof os.homedir === 'function' ? os.homedir() : void 0) || env.get('HOME')); |
||
35 | |||
36 | const windows = () => { |
||
37 | const priorityList = [ |
||
38 | typeof os.homedir === 'function' ? os.homedir() : void 0, |
||
39 | env.get('USERPROFILE'), |
||
40 | env.get('HOME'), |
||
41 | env.get('HOMEDRIVE') || env.get('HOMEPATH') |
||
42 | ? path.join(env.get('HOMEDRIVE') || '', env.get('HOMEPATH') || '') |
||
43 | : void 0, |
||
44 | ]; |
||
45 | return normalizePath(priorityList.find((v) => !isEmpty(v))); |
||
46 | }; |
||
47 | |||
48 | return isWinOS ? windows() : posix(); |
||
49 | } |
||
50 | |||
51 | function temp() { |
||
52 | function joinPathToBase(base: string | undefined, segments: readonly string[]) { |
||
53 | return base ? path.join(base, ...segments) : void 0; |
||
54 | } |
||
55 | |||
56 | function posix() { |
||
57 | const fallback = '/tmp'; |
||
58 | const priorityList = [ |
||
59 | typeof os.tmpdir === 'function' ? os.tmpdir() : void 0, |
||
60 | env.get('TMPDIR'), |
||
61 | env.get('TEMP'), |
||
62 | env.get('TMP'), |
||
63 | ]; |
||
64 | return normalizePath(priorityList.find((v) => !isEmpty(v))) || fallback; |
||
65 | } |
||
66 | |||
67 | function windows() { |
||
68 | const fallback = 'C:\\Temp'; |
||
69 | const priorityListLazy = [ |
||
70 | os.tmpdir, |
||
71 | () => env.get('TEMP'), |
||
72 | () => env.get('TMP'), |
||
73 | () => joinPathToBase(env.get('LOCALAPPDATA'), ['Temp']), |
||
74 | () => joinPathToBase(home(), ['AppData', 'Local', 'Temp']), |
||
75 | () => joinPathToBase(env.get('ALLUSERSPROFILE'), ['Temp']), |
||
76 | () => joinPathToBase(env.get('SystemRoot'), ['Temp']), |
||
77 | () => joinPathToBase(env.get('windir'), ['Temp']), |
||
78 | () => joinPathToBase(env.get('SystemDrive'), ['\\', 'Temp']), |
||
79 | ]; |
||
80 | const v = priorityListLazy.find((v) => v && !isEmpty(v())); |
||
81 | return (v && normalizePath(v())) || fallback; |
||
82 | } |
||
83 | |||
84 | return isWinOS ? windows() : posix(); |
||
85 | } |
||
86 | |||
87 | // eslint-disable-next-line functional/no-class |
||
88 | class OSPaths_ { |
||
89 | constructor() { |
||
90 | function OSPaths(): OSPaths { |
||
91 | return new OSPaths_() as OSPaths; |
||
92 | } |
||
93 | |||
94 | OSPaths.home = home; |
||
95 | OSPaths.temp = temp; |
||
96 | |||
97 | return OSPaths; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return { OSPaths: new OSPaths_() as OSPaths }; |
||
102 | } |
||
106 |